package com.ruoyi.jjyh.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.jjyh.domain.JjyhOrderGoods;
import com.ruoyi.jjyh.mapper.JjyhOrderMapper;
import com.ruoyi.jjyh.domain.JjyhOrder;
import com.ruoyi.jjyh.service.IJjyhOrderService;
import com.ruoyi.common.core.text.Convert;

/**
 * 易货订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-11-25
 */
@Service
public class JjyhOrderServiceImpl implements IJjyhOrderService 
{
    @Autowired
    private JjyhOrderMapper jjyhOrderMapper;

    /**
     * 查询易货订单
     * 
     * @param id 易货订单ID
     * @return 易货订单
     */
    @Override
    public JjyhOrder selectJjyhOrderById(String id)
    {
        return jjyhOrderMapper.selectJjyhOrderById(id);
    }

    @Override
    public JjyhOrder selectPcJjyhOrderGoodsById(String id) {
        return jjyhOrderMapper.selectPcJjyhOrderGoodsById(id);
    }

    /**
     * 查询易货订单列表
     * 
     * @param jjyhOrder 易货订单
     * @return 易货订单
     */
    @Override
    public List<JjyhOrder> selectJjyhOrderList(JjyhOrder jjyhOrder)
    {
        return jjyhOrderMapper.selectJjyhOrderList(jjyhOrder);
    }

    public List<JjyhOrder> selectOrderList(Map map){
        return jjyhOrderMapper.selectOrderList(map);
    }

    /**
     * 新增易货订单
     * 
     * @param jjyhOrder 易货订单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertJjyhOrder(JjyhOrder jjyhOrder)
    {
        jjyhOrder.setCreateTime(DateUtils.getNowDate());
        int rows = jjyhOrderMapper.insertJjyhOrder(jjyhOrder);
        insertJjyhOrderGoods(jjyhOrder);
        return rows;
    }

    /**
     * 修改易货订单
     * 
     * @param jjyhOrder 易货订单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateJjyhOrder(JjyhOrder jjyhOrder)
    {
        jjyhOrder.setUpdateTime(DateUtils.getNowDate());
        return jjyhOrderMapper.updateJjyhOrder(jjyhOrder);
    }

    /**
     * 删除易货订单对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteJjyhOrderByIds(String ids)
    {
        jjyhOrderMapper.deleteJjyhOrderGoodsByOrderIds(Convert.toStrArray(ids));
        return jjyhOrderMapper.deleteJjyhOrderByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除易货订单信息
     * 
     * @param id 易货订单ID
     * @return 结果
     */
    @Override
    public int deleteJjyhOrderById(String id)
    {
        jjyhOrderMapper.deleteJjyhOrderGoodsByOrderId(id);
        return jjyhOrderMapper.deleteJjyhOrderById(id);
    }

    /**
     * 新增订单商品表信息
     * 
     * @param jjyhOrder 易货订单对象
     */
    public void insertJjyhOrderGoods(JjyhOrder jjyhOrder)
    {
        List<JjyhOrderGoods> jjyhOrderGoodsList = jjyhOrder.getJjyhOrderGoodsList();

        if (StringUtils.isNotNull(jjyhOrderGoodsList))
        {
            List<JjyhOrderGoods> list = new ArrayList<JjyhOrderGoods>();
            for (JjyhOrderGoods jjyhOrderGoods : jjyhOrderGoodsList)
            {
                jjyhOrderGoods.setId(IdWorker.getId());
                list.add(jjyhOrderGoods);
            }
            if (list.size() > 0)
            {
                jjyhOrderMapper.batchJjyhOrderGoods(list);
            }
        }
    }
}
